package com.zhe.zheblogmaster.handler.admin;

import com.github.pagehelper.PageInfo;
import com.zhe.zheblogmaster.entity.Link;
import com.zhe.zheblogmaster.service.LinkService;
import com.zhe.zheblogmaster.util.ResultBody;
import com.zhe.zheblogmaster.util.ZheBlogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @author WHAOU
 * @date 2021/9/22
 * @email whaou@qq.com
 */
@Controller
@ResponseBody
@RequestMapping("/admin/link")
public class LinkHandler {
    @Autowired
    private LinkService linkService;

    @GetMapping("/list")
    public ResultBody<PageInfo<Link>> getLinkPageInfo(@RequestParam("pageNum") Integer pageNum) {
        PageInfo<Link> pageInfo = linkService.getLinkPageInfo(pageNum);
        return ResultBody.successWithData(pageInfo);
    }

    @PostMapping("/add")
    public ResultBody<String> addLink(@RequestParam("linkType") Byte type,
                                      @RequestParam("linkName") String name,
                                      @RequestParam("linkUrl") String url,
                                      @RequestParam("linkDescription") String description,
                                      @RequestParam("linkRank") Integer rank) {
        if (type == null || type < 0) {
            return ResultBody.error("请选择正确的友链类型");
        }
        if (rank == null || rank < 0) {
            return ResultBody.error("请输入正确的排序值");
        }
        if (!StringUtils.hasLength(name)) {
            return ResultBody.error("网站名称不能为空");
        }
        if (!StringUtils.hasLength(url)) {
            return ResultBody.error("网站地址不能为空");
        }
        if (!StringUtils.hasLength(description)) {
            return ResultBody.error("网站描述不能为空");
        }
        Link link = new Link();
        link.setLinkName(name);
        link.setLinkRank(rank);
        link.setLinkType(type);
        link.setLinkUrl(url);
        link.setLinkDescription(description);
        link.setCreateTime(ZheBlogUtils.getNow());

        int count = linkService.addLink(link);
        if (count > 0) {
            return ResultBody.successWithoutData();
        } else {
            return ResultBody.error("添加失败");
        }
    }

    @GetMapping("/info/{id}")
    public ResultBody<Link> getLinkInfo(@PathVariable("id") Integer id) {
        Link link = linkService.selectByPrimaryKey(id);
        return ResultBody.successWithData(link);
    }

    @PostMapping("/update")
    public ResultBody<String> updateLink(@RequestParam("linkId") Integer id,
                                         @RequestParam("linkType") Byte type,
                                         @RequestParam("linkName") String name,
                                         @RequestParam("linkUrl") String url,
                                         @RequestParam("linkDescription") String description,
                                         @RequestParam("linkRank") Integer rank) {
        Link tempLink = linkService.selectByPrimaryKey(id);
        if (tempLink == null) {
            return ResultBody.error("不存在该数据");
        }
        if (type == null || type < 0) {
            return ResultBody.error("请选择正确的友链类型");
        }
        if (rank == null || rank < 0) {
            return ResultBody.error("请输入正确的排序值");
        }
        if (!StringUtils.hasLength(name)) {
            return ResultBody.error("网站名称不能为空");
        }
        if (!StringUtils.hasLength(url)) {
            return ResultBody.error("网站地址不能为空");
        }
        if (!StringUtils.hasLength(description)) {
            return ResultBody.error("网站描述不能为空");
        }
        tempLink.setLinkType(type);
        tempLink.setLinkRank(rank);
        tempLink.setLinkName(name);
        tempLink.setLinkUrl(url);
        tempLink.setLinkDescription(description);
        int count = linkService.updateByPrimaryKeySelective(tempLink);
        if (count > 0) {
            return ResultBody.successWithoutData();
        } else {
            return ResultBody.error("修改失败");
        }
    }

    @PostMapping("/delete")
    public ResultBody<Integer> deleteLink(@RequestBody Integer[] ids) {
        if (ids == null || ids.length < 1) {
            return ResultBody.error("友链id数据组为空");
        }
        int count = linkService.deleteBatch(ids);
        if (count > 0) {
            return ResultBody.successWithData(count);
        } else {
            return ResultBody.error("删除失败");
        }
    }
}
